﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TFVA10
    {
        public TFVA10()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAFARBEN();
            DataContext = MV;
            InitializeComponent();
            this.Name = "色号管理";
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwVF = dwMaster;
            if (dwMaster != null)
            {
                FARBGRP = dwMaster["VAFFARBGRP"].ToString();
                PRART = dwMaster["VAAARTST"].ToString().PadRight(1) + TMV.VAFPART + TMV.VAFPVAR.PadRight(3) + TMV.VAFFFARBE + TMV.VAFBESCHICH + TMV.VAFDESS + TMV.VAFCOLORIT + TMV.VAFPGROE;
                KEY = TMV.VAFKOLL + dwMaster["VAFART"].ToString() + dwMaster["VAFFARBE"].ToString();
                TXTISIND = KEY.PadRight(60);
            }
            else
            {
                PRART = "";
                KEY = "";
                TXTISIND = "";
            }
        }
        public override void OnARTChg(string des)
        {
            base.OnARTChg(des);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            try
            { 
                dwData = TBMain.NewRow();
                dwData["VAFMANDANT"] = app.Mandant;
                dwData["VAFKOLL"] = KOLL.Trim();
                dwData["VAFART"] = ART.Trim();
                dwData["VAFFARBE"] = FARBE.Trim();
                dwData["VAFFFARBE"] = DES.Trim();
                SResult = TMV.QVAF05(dwData);
                MergeData(SResult, false);
                base.QueryCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnAppModeChg()
        {
            switch (app.company)
            {
                case "TST":
                    switch (app.AppMode)
                    {
                        case "TISSUE":
                            IsSelfColor = false;
                            break;
                        case "TMODE":
                            IsSelfColor = true;
                            break;

                    }
                    break;
            }
            base.OnAppModeChg();
        }
        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {

                if (!IsNew)
                {
                    IsNew = true;
                    VAFFARBE = "";
                    EXFARBE = "";
                    return;
                }

                DataRow dwData;
                if (ART.Trim().Length == 0)
                {
                    throw new Exception("请先选择品种");

                }
                if (!TBMain.Columns.Contains("VAAARTST"))
                    throw new Exception("请先执行查询");
                if (IsByCards)
                {
                    if (COLORS.Trim().Length == 0)
                    {
                        throw new Exception("请先输入色号");
                    }
                    string[] split = COLORS.Split(new char[] { ';' });
                    foreach (string str in split)
                    {
                        dwData = TBMain.NewRow();
                        TMV.SetUpNewRow(ref dwData);
                        dwData["VAFKOLL"] = KOLL;
                        dwData["VAFART"] = ART;
                        dwData["VAFFARBE"] = str.PadRight(10).Substring(0, 10);
                        Insert(dwData);
                    }
                    IsByCards = false;
                    return;
                }
                if (!IsSelfColor)
                {
                    VAFFARBE = EXFARBE;
                }
                else
                {
                    if (VAFFARBE.Trim().Length == 0)
                    {
                        throw new Exception("请先输入色号");

                    }
                }
                if (EXFARBE.Trim().Length == 0)
                {
                    throw new Exception("请先输入生产色号");
                }
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                dwData["VAFKOLL"] = KOLL;
                dwData["VAFART"] = ART;
                dwData["VAAARTST"] = ARTST;
                dwData["VAFFARBE"] = Helper.Left(VAFFARBE.PadRight(10), 10);
                dwData["VAFFFARBE"] = Helper.Left(EXFARBE.PadRight(10), 10);
                Insert(dwData);
                base.NewCommand(sender, e);
                VAFFARBE = "";
                IsNew = false;

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (!IsCopy)
                {
                    IsCopy = true;
                    EXFARBE = "";
                    return;
                }
                if (!IsSelfColor)
                {
                    VAFFARBE = EXFARBE;
                }
                else
                {
                    if (VAFFARBE.Trim().Length == 0)
                    {
                        MessageBox.Show("请先输入色号");
                        return;
                    }
                }
                if (EXFARBE.Trim().Length == 0)
                {
                    MessageBox.Show("请先输入生产色号");
                    return;
                }
                if (dwMaster == null)
                    return;
                if (VAFFARBE.Trim().Length == 0)
                {
                    MessageBox.Show("请先输入色号");
                    return;
                }
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwMaster.ItemArray;
                dwData["VAFFARBE"] = VAFFARBE;
                dwData["VAFFFARBE"] = dwData["VAFFARBE"];
                Insert(dwData);
                IsCopy = false;
                EXFARBE = "";
                base.CopyCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnEXAUFChg(string vlaue)
        {
            if (EXAUF.Length < 29)
                return;
            KOLL = EXAUF.Substring(0, 5);
            ART = EXAUF.Substring(5, 24);
            if (!IsLoaded)
                return;
            base.OnEXAUFChg(vlaue);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (dwVA != null)
            {
                dwMaster["VAFPART"] = dwVA["VAADART"];
                dwMaster["VAFPLART"] = dwVA["VAADART"];
                dwMaster["VAFPART"] = dwVA["VAADART"];
                dwMaster["VAFPVAR"] = dwVA["VAADVAR"];
            }
            base.EditCommand(sender, e);
        }
        public override void OnFARBEChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (FARBE == "")
                return;
            TMV.VAFFFARBE = FARBE;
            base.OnFARBEChg();
        }
        public override void OnDESSChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.VAFDESS = DESS;
            base.OnDESSChg();
        }
        public override void OnCOLORITChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.VAFCOLORIT = COLORIT;
            base.OnCOLORITChg();
        }
        public override void OnFINISHChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (FINISH == "")
                return;
            TMV.VAFBESCHICH = FINISH;
            base.OnFINISHChg();
        }
        public override void OnFSIZEChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.VAFPGROE = FSIZE;
            base.OnFSIZEChg();
        }

    }
}
